package project.messagestack.GUI;

import project.messagestack.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MailAdder extends Activity {

  public static final String TAG = "MailAdder";
  private EditText mAddMailLogin;
  private EditText mAddMailPass;
  private EditText mAddMailSerwer;
  private EditText mAddMailPort;
  private Button mAddMailButton;
  private RadioButton rb1;
  private RadioButton rb2;
  private int index;
  private Intent intent;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_mail_view);
    mAddMailButton = (Button) findViewById(R.id.addMailButtonOK);
    mAddMailPort = (EditText) findViewById(R.id.addMailPort);
    mAddMailSerwer = (EditText) findViewById(R.id.addMailSerwer);
    mAddMailPass = (EditText) findViewById(R.id.addMailPass);
    mAddMailLogin = (EditText) findViewById(R.id.addMailLogin);
    rb1 = (RadioButton) findViewById(R.id.option1);
    rb2 = (RadioButton) findViewById(R.id.option2);
    if (getIntent().hasExtra("login")) {
      mAddMailLogin.setText(getIntent().getStringExtra("login"));
    }
    if (getIntent().hasExtra("pass")) {
      mAddMailPass.setText(getIntent().getStringExtra("pass"));
    }
    if (getIntent().hasExtra("serwer")) {
      mAddMailSerwer.setText(getIntent().getStringExtra("serwer"));
    }
    if (getIntent().hasExtra("port")) {
      mAddMailPort.setText(getIntent().getStringExtra("port"));
    }
    if (getIntent().hasExtra("index")) {
      index=getIntent().getIntExtra("index", -1);
    }
    if(getIntent().hasExtra("type")){
      String typ = getIntent().getStringExtra("type");
      if(typ.equals("IMAP")){
        rb1.setChecked(true);
        rb2.setChecked(false);
      }else{
        rb2.setChecked(true);
        rb1.setChecked(false);
      }
    }

    mAddMailButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        if (createAcount()) {
          finish();
        }
      }
    });
    intent = new Intent();
    setResult(Activity.RESULT_OK, intent);
  }

  protected boolean createAcount() {
    intent = new Intent();
    String login = mAddMailLogin.getText().toString();
    String pass = mAddMailPass.getText().toString();
    String port = mAddMailPort.getText().toString();
    String serwer = mAddMailSerwer.getText().toString();
    String type;
    if(rb1.isChecked()){
      type="IMAP";
    }
    else{
      type="POP3";
    }
    try {
      int p = Integer.parseInt(port);
      intent.putExtra("login", login);
      intent.putExtra("pass", pass);
      intent.putExtra("port", p);
      intent.putExtra("serwer", serwer);
      intent.putExtra("index", index);
      intent.putExtra("type", type);
      setResult(Activity.RESULT_OK, intent);
    } catch (NumberFormatException e) {
      Toast.makeText(this, R.string.mailToastPoerError, Toast.LENGTH_LONG)
          .show();
      return false;
    }
    return true;

  }
}
